<?php
/**
 * Created by PhpStorm.
 * User: runyan
 * Date: 2017/10/7
 * Time: 14:23
 */

namespace common\components;


class Helper
{
    public static function formatExcel($data)
    {
        $arr = [];
        foreach ($data as $k => $v) {
            if ($k == 0) {
                continue;
            }
            foreach ($data[0] as $i => $j) {
                $arr[$k][$j] = $v[$i];
            }
        }
        return $arr;
    }

    public static function formatData($data)
    {
        $arr = [];
        foreach ($data as $k => $v) {
            if ($k == 0) {
                continue;
            }
            $arr[$k] = $v;
            $arr[$k]['created_at'] = time();
            $arr[$k]['updated_at'] = time();
        }
        return $arr;
    }

    //氨基酸表
    public static function formatAmino($data, $sheetName)
    {
        $arr = [];
        foreach ($data as $k => $v) {
            $arr[$k]['sheet_name'] = $sheetName;
            $arr[$k]['sn'] = $v[0];
            array_shift($v);
            $arr[$k]['data'] = json_encode($v);

        }
        return $arr;
    }

    /**
     * 可逆的字符串加密函数[无依赖简版]
     * @param int $txtStream 待加密的字符串内容
     * @param int $password 加密密码
     * @return string 加密后的字符串
     */
    public static function enCrypt($txtStream, $password = 'ENCRYPT_KEY')
    {
        //密锁串，不能出现重复字符，内有A-Z,a-z,0-9,/,=,+,_,
        $lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef+IJK6/7nopqr89LMmGH012345uv';
        //随机找一个数字，并从密锁串中找到一个密锁值
        $lockLen = strlen($lockstream);
        $lockCount = rand(0, $lockLen - 1);
        $randomLock = $lockstream[$lockCount];
        //结合随机密锁值生成MD5后的密码
        $password = md5($password . $randomLock);
        //开始对字符串加密
        $txtStream = base64_encode($txtStream);
        $tmpStream = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txtStream); $i++) {
            $k = ($k == strlen($password)) ? 0 : $k;
            $j = (strpos($lockstream, $txtStream[$i]) + $lockCount + ord($password[$k])) % ($lockLen);
            $tmpStream .= $lockstream[$j];
            $k++;
        }
        return $tmpStream . $randomLock;

    }

    /**
     * 可逆的字符串解密函数[无依赖简版]
     * @param int $txtStream 待加密的字符串内容
     * @param int $password 解密密码
     * @return string 解密后的字符串
     */
    public static function deCrypt($txtStream, $password = 'ENCRYPT_KEY')
    {
        //密锁串，不能出现重复字符，内有A-Z,a-z,0-9,/,=,+,_,
        $lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef+IJK6/7nopqr89LMmGH012345uv';

        $lockLen = strlen($lockstream);
        //获得字符串长度
        $txtLen = strlen($txtStream);
        //截取随机密锁值
        $randomLock = $txtStream[$txtLen - 1];
        //获得随机密码值的位置
        $lockCount = strpos($lockstream, $randomLock);
        //结合随机密锁值生成MD5后的密码
        $password = md5($password . $randomLock);
        //开始对字符串解密
        $txtStream = substr($txtStream, 0, $txtLen - 1);
        $tmpStream = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txtStream); $i++) {
            $k = ($k == strlen($password)) ? 0 : $k;
            $j = strpos($lockstream, $txtStream[$i]) - $lockCount - ord($password[$k]);
            while ($j < 0) {
                $j = $j + ($lockLen);
            }
            $tmpStream .= $lockstream[$j];
            $k++;
        }
        return base64_decode($tmpStream);
    }


}